<?php
/**
 * @version		$Id: view.html.php 11408 2009-01-10 02:24:28Z ian $
 * @package		Joomla
 * @subpackage	Content
 * @copyright	Copyright (C) 2005 - 2008 Open Source Matters. All rights reserved.
 * @license		GNU/GPL, see LICENSE.php
 * Joomla! is free software. This version may have been modified pursuant to the
 * GNU General Public License, and as distributed it includes or is derivative
 * of works licensed under the GNU General Public License or other free or open
 * source software licenses. See COPYRIGHT.php for copyright notices and
 * details.
 */

// Check to ensure this file is included in Joomla!
defined('_JEXEC') or die( 'Restricted access' );

require_once (JPATH_COMPONENT.DS.'view.php');

/**
 * HTML View class for the Content component
 *
 * @package		Joomla
 * @subpackage	Content
 * @since 1.5
 */
class ContentViewTapa extends ContentView
{
	function display($tpl = null)
	{
		global $mainframe, $option;

		// Initialize some variables
		$user		=& JFactory::getUser();
		$uri 		=& JFactory::getURI();
		$document	=& JFactory::getDocument();
		$pathway	=& $mainframe->getPathway();

		// Get the menu item object
		$menus = &JSite::getMenu();
		$menu  = $menus->getActive();

		// Get the page/component configuration
		$params = clone($mainframe->getParams('com_content'));


		$contentConfig = &JComponentHelper::getParams('com_content');
		$params->def('show_page_title', 	$contentConfig->get('show_title'));

		$items		= & $this->get( 'Data' );
		$category	= & $this->get( 'Category' );

		//set breadcrumbs
		if(is_object($menu) && $menu->query['view'] != 'tapa') {
			$pathway->addItem($category->title, '');
		}


		// Set page title per category
		// because the application sets a default page title, we need to get it
		// right from the menu item itself
		if (is_object( $menu )) {
			$menu_params = new JParameter( $menu->params );
			if (!$menu_params->get( 'page_title')) {
				$params->set('page_title',	$category->title);
			}
		} else {
			$params->set('page_title',	$category->title);
		}
		$document->setTitle( $params->get( 'page_title' ) );
	
		// Request variables
		$layout     = JRequest::getCmd('layout');
		$task		= JRequest::getCmd('task');

		$this->assignRef('items',		$items);
		$this->assignRef('params',		$params);


		parent::display($tpl);
	}

	function &getItems()
	{
		global $mainframe;

		//create select lists
		$user	= &JFactory::getUser();

		return $this->items;
	}

}

global $mainframe;
$params = clone($mainframe->getParams('com_content'));
// Set the path definitions
if(!defined('MOD_MINIFRONTPAGE_BASE')) {
	define('MOD_MINIFRONTPAGE_BASE', JPATH_SITE.DS.$params->get('image_path', 'images'.DS.'stories'));
	$document =& JFactory::getDocument();
	$document->addCustomTag( '<link rel="stylesheet" type="text/css" href="'. JURI::base() . 'modules/mod_minifrontpage/css/style.css" title="default" />' );
}
if(!defined('MOD_MINIFRONTPAGE_BASEURL')) {
	define('MOD_MINIFRONTPAGE_BASEURL', JURI::base().$params->get('image_path', 'images/stories'));
}

if(!defined('MOD_MINIFRONTPAGE_THUMB_BASE')) {
	define('MOD_MINIFRONTPAGE_THUMB_BASE', JPATH_SITE.DS.$params->get('image_path', 'images'.DS.'stories'.DS.'minifp'));
}
if(!defined('MOD_MINIFRONTPAGE_THUMB_BASEURL')) {
	define('MOD_MINIFRONTPAGE_THUMB_BASEURL', JURI::base().$params->get('image_path', 'images/stories/minifp/'));
}

if(!file_exists(MOD_MINIFRONTPAGE_THUMB_BASE)) {
	if(mkdir(MOD_MINIFRONTPAGE_THUMB_BASE)) {
		JPath::setPermissions(MOD_MINIFRONTPAGE_THUMB_BASE, '0777');
	}
} else {
	if(!is_dir(MOD_MINIFRONTPAGE_THUMB_BASE)) {
		if(mkdir(MOD_MINIFRONTPAGE_THUMB_BASE)) {
			JPath::setPermissions(MOD_MINIFRONTPAGE_THUMB_BASE, '0777');
		}
	}
}
		
// if there's no image in an article, give it a default one - change image name here if you have one
if(!defined('MOD_MINIFRONTPAGE_DEFAULT_BASE')) {
	define('MOD_MINIFRONTPAGE_DEFAULT_BASE', JPATH_SITE.DS.$params->get('image_path', 'modules'.DS.'mod_minifrontpage'.DS.'images'));
}
if(!defined('MOD_MINIFRONTPAGE_DEFAULT_BASEURL')) {
	define('MOD_MINIFRONTPAGE_DEFAULT_BASEURL', JURI::base().$params->get('image_path', 'modules/mod_minifrontpage/images'));
}
if(!defined('MOD_MINIFRONTPAGE_DEFAULT_IMAGE')) {
	define('MOD_MINIFRONTPAGE_DEFAULT_IMAGE', 'default.gif');
}

// Get Module Parameters (needed by default.php)
$moduleclass_sfx 		 = $params->get( 'moduleclass_sfx', '' );
$columns 						 = intval( $params->get( 'columns', 1 ) );
$cat_title 		 			 = intval( $params->get( 'cat_title', 0 ) );
$cat_title_link 		 = intval( $params->get( 'cat_title_link', 0 ) );
$show_title 	 			 = intval( $params->get( 'show_title', 1 ) );
$num_intro 					 = intval( $params->get( 'num_intro', 1) );
$columns 						 = intval( $params->get( 'columns', 1 ) );
$count 							 = intval( $params->get( 'count', 5 ) );
$loadorder 		 			 = intval( $params->get( 'loadorder', 1 ) );
$title_link 	 			 = intval( $params->get( 'title_link', 1 ) );
$show_author     		 = intval( $params->get( 'show_author', 0 ) );
$show_author_type  	 = intval( $params->get( 'show_author_type', 0 ) );
$show_date 	     		 = intval( $params->get( 'show_date', 0 ) );
//$fulllink 		 			 = $params->get( 'fulllink','' );
//$header_title_links  = $params->get( 'header_title_links', "" );
$limit 			 				 = intval( $params->get( 'limit', 200 ) );
$thumb_embed 				 = intval( $params->get( 'thumb_embed', 0 ) );
$thumb_width 				 = intval( $params->get( 'thumb_width', 32 ) );
$thumb_height 			 = intval( $params->get( 'thumb_height', 32 ) );
$aspect 						 = intval( $params->get( 'aspect', 0 ) );
$thumb_embed_default = intval( $params->get( 'thumb_embed_default', 1 ) );

if ($columns > 4) $columns = 4; // limit number of columns
$anotherlink = ( ($count - $num_intro) ==0 ) ? 0 : 1 ;

$sep = " | "; // separator between articles date and creator

// css classes - hardcoded
$class_introtitle 		= 'minifp-introtitle';
$class_date 					= "minifp-date";
$class_author 				= "minifp-date";
$class_another_links 	= "minifp-anotherlinks";
$class_minifulllink 	= "minifp-full_link";

$class_categoria 			= 'minifp-anotherlinks';

//Function 
//added by remuz, edited by Jerry Wijaya
if (!function_exists("fptn_thumb_size")) {
function fptn_thumb_size($file, $wdth, $hgth, &$image, &$xtra, $class, $aspect) {
	$pos = stripos($file, 'http://');

	if($class!='') $xtra .= ' class="'.$class.'"';

	if($file ==""){
		$file = MOD_MINIFRONTPAGE_DEFAULT_IMAGE;
		$path = MOD_MINIFRONTPAGE_DEFAULT_BASE;
		$site = MOD_MINIFRONTPAGE_DEFAULT_BASEURL;
	}else{
		$path = MOD_MINIFRONTPAGE_THUMB_BASE;
		$site = MOD_MINIFRONTPAGE_THUMB_BASEURL;
	}
		
	// Find the extension of the file
	if($pos === false) {
		$ext = substr(strrchr(basename(JPATH_SITE.$file), '.'), 1);
	} else {
		$ext = substr(strrchr(basename($file), '.'), 1);
	}

	$thumb = str_replace('.'.$ext, '_thumb.'.$ext, $file);
	$thumb = explode("/", $thumb);
	$thumb = $thumb[count($thumb)-1];
	$thumb = explode("\\", $thumb);
	$thumb = $thumb[count($thumb)-1];

	$image = '';

	$image_path = $path.DS.$thumb;
	$image_site = $site."/".$thumb;

	$found = false;
	if (file_exists($image_path)){
		$size = '';
		$wx = $hy = 0;
		if (function_exists( 'getimagesize' )){
			$size = @getimagesize( $image_path );
			if (is_array( $size )){
				$wx = $size[0];
				$hy = $size[1];
				$size = 'width="'.$wx.'" height="'.$hy.'"';
			}
	  }

		$found = true;
		$size = 'width="'.$wx.'" height="'.$hy.'"';
		$image= '<img src="'.$image_site.'" '.$size.$xtra.' />';
	}
	
	if (!$found){
		$size = '';
		$wx = $hy = 0;
		if($pos === false) {
			if(file_exists(MOD_MINIFRONTPAGE_BASE.DS.$file)) {
				$sFile = MOD_MINIFRONTPAGE_BASE.DS.$file;
			} else {
				$sFile = MOD_MINIFRONTPAGE_DEFAULT_BASE.DS.$file;
			}
		} else {
			if(file_exists($file)) {
				$sFile = $file;
			} else {
				$sFile = MOD_MINIFRONTPAGE_DEFAULT_BASE.DS.MOD_MINIFRONTPAGE_DEFAULT_IMAGE;
			}
		}
		
		$size = @getimagesize( $sFile );
		
		if (is_array( $size )){
			$wx = $size[0];
			$hy = $size[1];
		}

		fptn_calcsize($wx, $hy, $wdth, $hgth, $aspect);

		switch ($ext){
			case 'jpg':
			case 'jpeg':
			case 'png':
				if($pos === false) {
					fptn_thumbit($sFile,$image_path,$ext,$wdth,$hgth);
				} else {
					fptn_thumbit($file,$image_path,$ext,$wdth,$hgth);
				}
				$size = 'width="'.$wdth.'" height="'.$hgth.'"';
				$image= '<img  src="'.$image_site.'" '.$size.$xtra.' />';
				break;
			case 'gif':
				if (function_exists("imagegif")) {
					if($pos === false) {
						fptn_thumbit($sFile,$image_path,$ext,$wdth,$hgth);
					} else {
						fptn_thumbit($file,$image_path,$ext,$wdth,$hgth);
					}
					$size = 'width="'.$wdth.'" height="'.$hgth.'"';
					$image= '<img src="'.$image_site.'" '.$size.$xtra.' />';
					break;
        }
			default:
				$size = 'width="'.$wdth.'" height="'.$hgth.'"';
				if($pos === false) {
					$image= '<img src="'.MOD_MINIFRONTPAGE_BASEURL."/".$file.'" '.$size.$xtra.' />';
				} else {
					$image= '<img src="'.$file.'" '.$size.$xtra.' />';
				}
				break;
		}
	}
}
}

if (!function_exists("fptn_thumbIt")) {
function fptn_thumbIt ($file, $thumb, $ext, &$new_width, &$new_height) {
	$img_info = getimagesize ( $file );
	$orig_width = $img_info[0];
	$orig_height = $img_info[1];
		
	if($orig_width<$new_width || $orig_height<$new_height){
		$new_width = $orig_width;
		$new_height = $orig_height;
	}
		
	switch ($ext) {
		case 'jpg':
		case 'jpeg':
			$im  = imagecreatefromjpeg($file);
			$tim = imagecreatetruecolor ($new_width, $new_height);
			fptn_ImageCopyResampleBicubic($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
			imagedestroy($im);
	
			imagejpeg($tim, $thumb, 75);
			imagedestroy($tim);
			break;

		case 'png':
			$im  = imagecreatefrompng($file);
			$tim = imagecreatetruecolor ($new_width, $new_height);
			fptn_ImageCopyResampleBicubic($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
			imagedestroy($im);
			imagepng($tim, $thumb, 9);
			imagedestroy($tim);
			break;

		case 'gif':
			if (function_exists("imagegif")) {
				$im  = imagecreatefromgif($file);
				$tim = imagecreatetruecolor ($new_width, $new_height);
				fptn_ImageCopyResampleBicubic($tim, $im, 0,0,0,0, $new_width, $new_height, $orig_width, $orig_height);
				imagedestroy($im);
				imagegif($tim, $thumb, 75);
				imagedestroy($tim);
    	}
			break;

			default:
				break;
	}
}
}

if (!function_exists("fptn_ImageCopyResampleBicubic")) {
function fptn_ImageCopyResampleBicubic (&$dst_img, &$src_img, $dst_x, $dst_y, $src_x, $src_y, $dst_w, $dst_h, $src_w, $src_h) {
	if ($dst_w==$src_w && $dst_h==$src_h) {
		$dst_img = $src_img;
		return;
	}

	ImagePaletteCopy ($dst_img, $src_img);
	$rX = $src_w / $dst_w;
	$rY = $src_h / $dst_h;
	$w = 0;
	for ($y = $dst_y; $y < $dst_h; $y++) {
		$ow = $w; $w = round(($y + 1) * $rY);
		$t = 0;
		for ($x = $dst_x; $x < $dst_w; $x++) {
			$r = $g = $b = 0; $a = 0;
			$ot = $t; $t = round(($x + 1) * $rX);
			for ($u = 0; $u < ($w - $ow); $u++) {
				for ($p = 0; $p < ($t - $ot); $p++) {
					$c = ImageColorsForIndex ($src_img, ImageColorAt ($src_img, $ot + $p, $ow + $u));
					$r += $c['red'];
   				$g += $c['green'];
   				$b += $c['blue'];
   				$a++;
   			}
			}
			
			if(!$a) $a = 1; {
				ImageSetPixel ($dst_img, $x, $y, ImageColorClosest ($dst_img, $r / $a, $g / $a, $b / $a));
			}
		}
	}
}
}

if (!function_exists("fptn_calcsize")) {
function fptn_calcsize($srcx, $srcy, &$forcedwidth, &$forcedheight, $aspect) {
	if ($forcedwidth > $srcx)  $forcedwidth = $srcx;
	if ($forcedheight > $srcy) $forcedheight = $srcy;
	if ( $forcedwidth <=0 && $forcedheight > 0) {
		$forcedwidth = round(($forcedheight * $srcx) / $srcy);
	} else if ( $forcedheight <=0 && $forcedwidth > 0) {
		$forcedheight = round(($forcedwidth * $srcy) / $srcx);
	} else if ( $forcedwidth/$srcx>1 && $forcedheight/$srcy>1) {
		//May not make an image larger!
		$forcedwidth = $srcx;
		$forcedheight = $srcy;
	}	else if ( $forcedwidth/$srcx<1 && $aspect) {
		//$forcedheight = round(($forcedheight * $forcedwidth) /$srcx);
		$forcedheight = round( ($srcy/$srcx) * $forcedwidth );
		$forcedwidth = $forcedwidth;
	}
}
}

//function added by Jerry Wijaya
if (!function_exists("fptn_limittext")) {
function fptn_limittext($text,$allowed_tags,$limit) {
	$strip = strip_tags($text);
	$endText = (strlen($strip) > $limit) ? "&nbsp;[&nbsp;...&nbsp;]" : ""; 
	$strip = substr($strip, 0, $limit);
	$striptag = strip_tags($text, $allowed_tags);
	$lentag = strlen($striptag);
	
	$display = "";
	
	$x = 0;
	$ignore = true;
	for($n = 0; $n < $limit; $n++) {
		for($m = $x; $m < $lentag; $m++) {
			$x++;
			if($striptag[$m] == "<") {
				$ignore = false;
			} else if($striptag[$m] == ">") {
				$ignore = true;
			}
			if($ignore == true) {
				if($strip[$n] != $striptag[$m]) {
					$display .= $striptag[$m];
				} else {
					$display .= $strip[$n];
					break;
				}
			} else {
				$display .= $striptag[$m];
			}
		}
	}
	return fix_tags ('<p>'.$display.$endText.'</p>');}
}

/* EL INFIERNO ABAJO!! */
//added by remush
//this fuction to show Thumbnail image
if (!function_exists("showThumb")) {
function showThumb($images,$image,$params,$link){
	$thumb_embed = intval( $params->get( 'thumb_embed', 1 ) );
	$thumb_width = intval( $params->get( 'thumb_width', 32 ) );
	$thumb_height = intval( $params->get( 'thumb_height', 32 ) );
	$aspect = intval( $params->get( 'aspect', 0 ) );
	// show thumbnail image	
	if ($thumb_embed == 1) {
		?><a href="<?php echo $link ?>"><?php
		if (!empty($images)) {
			$img = strtok($images,"|\r\n");
			$class="";
			$extra = ' align="left"  alt="article thumbnail" ';
			fptn_thumb_size($img, $thumb_width, $thumb_height, $image, $extra, $class, $aspect);
			echo  $image;
		} else if ($image !="") {
			$img = strtok($image,"|\r\n");
			$class="";
			$extra = ' align="left"  alt="article thumbnail" ';
			fptn_thumb_size($img, $thumb_width, $thumb_height, $image, $extra, $class, $aspect);
			echo  $image;
		} else {			
			$img = "";	
			$class="";
			$extra = ' align="left"  alt="article thumbnail" ';
			fptn_thumb_size($img, $thumb_width, $thumb_height, $image, $extra, $class, $aspect);
			echo  $image;			
		}
		?></a><?php				
	}
}
}

//added by Jerry Wijaya
//function taken from https://svn.typo3.org/TYPO3v4/Extensions/pdf_generator2/trunk/html2ps/xhtml.utils.inc.php
if(!function_exists('fix_tags')) {
function fix_tags($html) {
  $result = "";
  $tag_stack = array();

  // these corrections can simplify the regexp used to parse tags
  // remove whitespaces before '/' and between '/' and '>' in autoclosing tags
  $html = preg_replace("#\s*/\s*>#is","/>",$html);
  // remove whitespaces between '<', '/' and first tag letter in closing tags
  $html = preg_replace("#<\s*/\s*#is","</",$html);
  // remove whitespaces between '<' and first tag letter 
  $html = preg_replace("#<\s+#is","<",$html);

  while (preg_match("#(.*?)(<([a-z\d]+)[^>]*/>|<([a-z\d]+)[^>]*(?<!/)>|</([a-z\d]+)[^>]*>)#is",$html,$matches)) {
    $result .= $matches[1];
    $html = substr($html, strlen($matches[0]));

    // Closing tag 
    if (isset($matches[5])) { 
      $tag = $matches[5];

      if ($tag == $tag_stack[0]) {
        // Matched the last opening tag (normal state) 
        // Just pop opening tag from the stack
        array_shift($tag_stack);
        $result .= $matches[2];
      } elseif (array_search($tag, $tag_stack)) { 
        // We'll never should close 'table' tag such way, so let's check if any 'tables' found on the stack
        $no_critical_tags = !array_search('table',$tag_stack);
        if (!$no_critical_tags) {
          $no_critical_tags = (array_search('table',$tag_stack) >= array_search($tag, $tag_stack));
        };

        if ($no_critical_tags) {
          // Corresponding opening tag exist on the stack (somewhere deep)
          // Note that we can forget about 0 value returned by array_search, becaus it is handled by previous 'if'
          
          // Insert a set of closing tags for all non-matching tags
          $i = 0;
          while ($tag_stack[$i] != $tag) {
            $result .= "</{$tag_stack[$i]}> ";
            $i++;
          }; 
          
          // close current tag
          $result .= "</{$tag_stack[$i]}> ";
          // remove it from the stack
          array_splice($tag_stack, $i, 1);
          // if this tag is not "critical", reopen "run-off" tags
          $no_reopen_tags = array("tr","td","table","marquee","body","html");
          if (array_search($tag, $no_reopen_tags) === false) {
            while ($i > 0) {
              $i--;
              $result .= "<{$tag_stack[$i]}> ";
            }; 
          } else {
            array_splice($tag_stack, 0, $i);
          };
        };
      } else {
        // No such tag found on the stack, just remove it (do nothing in out case, as we have to explicitly 
        // add things to result
      };
    } elseif (isset($matches[4])) {
      // Opening tag
      $tag = $matches[4];
      array_unshift($tag_stack, $tag);
      $result .= $matches[2];
    } else {
      // Autoclosing tag; do nothing specific
      $result .= $matches[2];
    };
  };

  // Close all tags left
  while (count($tag_stack) > 0) {
    $tag = array_shift($tag_stack);
    $result .= "</".$tag.">";
  }	

  return $result;
}
}
?>
